#**************************************************************************
#*********************---------------------------------**********************
#********************|    Copyright (c) 2003 - 2023    |*********************
#********************|      by VIRE Technologies.        |*********************
#********************|       All Rights Reserved           |*********************
#*********************---------------------------------**********************
#**************************************************************************
#
# If any question, please write to VIRE Technologies for
# more information:
#			Techart Plaza, A16, No.30 XueYuan Road,
#			Haidian District Beijing 100083,
#			P.R. China
#
# For more, please visit company website: www.vire.cn
#**************************************************************************

set(VERSION_TEMPLATE_DIR "${CMAKE_CURRENT_LIST_DIR}/version")

# Generate a version header file for a target
# generate_version_header(<target>)

# Header will contain preprocessor definitions with version information that may be included
# by an application's resource file
macro(generate_version_header TARGET)
   set(options WITH_PRODUCT_PREFIX)
   set(oneValueArgs OUT_VERSION_FILE)
   set(multiValueArgs)
   cmake_parse_arguments(arg "${options}" "${oneValueArgs}"
                  "${multiValueArgs}" ${ARGN} )

   if(NOT DEFINED EWX_EXT_VERSION_MAJOR)
      message(FATAL_ERROR "EWX_EXT_VERSION_MAJOR, EWX_EXT_VERSION_MINOR, EWX_EXT_VERSION_PATCH, EWX_EXT_VERSION and EWX_RELEASE_VERSION must be defined")
   endif()

   # Use upper case of target name as prefix for all preprocessor definitions
   string(TOUPPER ${TARGET} VERSION_PREFIX)

   # Default all definitions to the corresponding EWX variable
   set(EWX_${VERSION_PREFIX}_VERSION_MAJOR ${EWX_EXT_VERSION_MAJOR})
   set(EWX_${VERSION_PREFIX}_VERSION_MINOR ${EWX_EXT_VERSION_MINOR})
   set(EWX_${VERSION_PREFIX}_VERSION_PATCH ${EWX_EXT_VERSION_PATCH})
   set(EWX_${VERSION_PREFIX}_VERSION ${EWX_EXT_VERSION})
   set(EWX_${VERSION_PREFIX}_RELEASE_VERSION ${EWX_RELEASE_VERSION})

   set(EWX_${VERSION_PREFIX}_FILEVERSION "EWX_${VERSION_PREFIX}_VERSION_MAJOR,EWX_${VERSION_PREFIX}_VERSION_MINOR,EWX_${VERSION_PREFIX}_VERSION_PATCH")
   set(EWX_${VERSION_PREFIX}_FILEVERSION_STR "EWX_${VERSION_PREFIX}_VERSION")
   set(EWX_${VERSION_PREFIX}_PRODUCTVERSION "EWX_${VERSION_PREFIX}_FILEVERSION")
   set(EWX_${VERSION_PREFIX}_PRODUCTVERSION_STR "EWX_${VERSION_PREFIX}_RELEASE_VERSION")

   set(EWX_${VERSION_PREFIX}_COMPANYNAME_STR "\"EWX_${EWX_COMPANYNAME}\"")
   set(EWX_${VERSION_PREFIX}_LEGALCOPYRIGHT_STR "\"${EWX_LEGALCOPYRIGHT}\"")
   set(EWX_${VERSION_PREFIX}_LEGALTRADEMARKS1_STR "\"${EWX_LEGALTRADEMARKS}\"")
   set(EWX_${VERSION_PREFIX}_LEGALTRADEMARKS2_STR "EWX_${VERSION_PREFIX}_LEGALTRADEMARKS1_STR")
   set(EWX_${VERSION_PREFIX}_COMPANYDOMAIN_STR "\"${EWX_COMPANYDOMAIN}\"")


   # add by luli:
   # 生成对应的产品名称的Version替换处理
   IF(arg_WITH_PRODUCT_PREFIX)
      #生成新的Version Info File...
      SET(PRODUCT_PREFIX_NAME ${EWX_PRODUCT_TYPE})
      set(VERSION_INFO_FILE_SRC "${CMAKE_CURRENT_SOURCE_DIR}/version_info.cmake")
      
      # If the target has its own version_info.cmake, include it and possibly override any
      # variables defined above
      set(VERSION_INFO_FILE "${PROJECT_BINARY_DIR}/${PROJECT_NAME}_version_info.cmake")
      configure_file(${VERSION_INFO_FILE_SRC} ${VERSION_INFO_FILE})
   ELSE()
      # If the target has its own version_info.cmake, include it and possibly override any
      # variables defined above
      set(VERSION_INFO_FILE "${CMAKE_CURRENT_SOURCE_DIR}/version_info.cmake")
   ENDIF()

   if (EXISTS ${VERSION_INFO_FILE})
      include(${VERSION_INFO_FILE})
   endif()

   IF(WIN32)
      SET(src_config_in_file "${VERSION_TEMPLATE_DIR}/version_defines.hpp.in")
   ELSEIF(UNIX)
      SET(src_config_in_file "${VERSION_TEMPLATE_DIR}/version_defines.hpp.in")
      set(dst_config_in_file "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}_version_defines.hpp.in")
      #需要先对文件进行转码后再进行configure_file处理
      #将GBK的编绎转换为UTF-8
      execute_process(
         COMMAND iconv -f GBK -t UTF-8 ${src_config_in_file} -o ${dst_config_in_file}
      )
      #message(FATAL_ERROR "UNIX ICONV File Stopped...${dst_config_in_file}")
      #重新设置新生成的模板
      SET(src_config_in_file ${dst_config_in_file})
   ELSE()
      message(FATAL_ERROR "NO Implement....")
   ENDIF()

   # Configure the version header
   set(VERSION_HEADER "${CMAKE_CURRENT_BINARY_DIR}/include/${TARGET}_version_defines.hpp")
   configure_file("${src_config_in_file}" ${VERSION_HEADER})
   set(${arg_OUT_VERSION_FILE} ${VERSION_HEADER})
   # Add generated version header to the sources and include directories of the target
   target_sources(${TARGET} PRIVATE ${VERSION_HEADER})
   #target_include_directories(${TARGET} PUBLIC "${CMAKE_CURRENT_BINARY_DIR}/include")
   target_include_directories(${TARGET} BEFORE
    PUBLIC
      "$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>"
   )
endmacro()

# Generate a MSVC resource (.rc) file for a target
# generate_resource(<target> [ICON_PATH])
#
# Can only be used if generate_version_header is called first to generate the version header,
# which will be included by the resource file.
# Optional argument ICON_PATH specifies the path to the target's ICON resource
macro(generate_resource TARGET)

   # Set variables for substitution in input file that is configured
   if (${ARGC} GREATER 1)
      set(ICON_RESOURCE ${ARGV1}) # Optional argument for icon
      #message(STATUS  "=====TARGET[${TARGET}] Use ICON_RESOURCE=${ICON_RESOURCE}")
   endif()
   set(VERSION_HEADER_PREFIX ${TARGET})
   string(TOUPPER ${TARGET} VERSION_PREFIX)

   # Configure the resource file and add it to the sources of target
   set(RESOURCE_FILE "${CMAKE_CURRENT_BINARY_DIR}/${TARGET}.rc")
   configure_file("${VERSION_TEMPLATE_DIR}/version_info.rc.in" ${RESOURCE_FILE})
   target_sources(${TARGET} PRIVATE ${RESOURCE_FILE})

endmacro()
